#pragma once
#include <cstdlib>
#include <new>
#include <iostream>
#include <boost/thread.hpp>

namespace Core{
	class MemoryManager{
	public:
		static size_t getTotalDynamicAllocation(){
			return m_totalAllocation;
		}

		static size_t 		m_totalAllocation;
		static boost::mutex	m_allocatorMutex;
	};

	// Class overloading new and delete
	class MemTrack{
	public:
		MemTrack(){}

		void* operator new (size_t size){
			m_allocationSize = size;

			MemoryManager::m_allocatorMutex.lock();
				void *p = malloc(size);
				MemoryManager::m_totalAllocation += m_allocationSize;
			MemoryManager::m_allocatorMutex.unlock();

			if(p == 0){
				LOG_ERROR("Allocation failure.");
				assert(0);
			}

			return p;
		}

		void operator delete (void *p){
			MemoryManager::m_allocatorMutex.lock();
				free(p);
				p = NULL;
				MemoryManager::m_totalAllocation -= m_allocationSize;
			MemoryManager::m_allocatorMutex.unlock();
		}

		static size_t m_allocationSize;
	};
}